package com.puyuba.puyuba.view.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.puyuba.puyuba.R;
import com.puyuba.puyuba.SkinApplication;
import com.puyuba.puyuba.bean.FindTanListBean;
import com.puyuba.puyuba.network.UserApi;
import com.puyuba.puyuba.view.adapter.FindTransactionAdatper;
import com.pyb.java.basemodule.BaseFragment;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.cache.bean.UserInfoBean;
import com.pyb.java.commonutil.ToastHelper;
import com.pyb.java.commonutil.utils.NetUtil;
import com.pyb.java.network.ResponseEntity;
import com.pyb.java.network.RetrofitManger;
import com.pyb.java.network.observers.ResponseObserve;
import com.pyb.java.network.scheduler.RxSchedulers;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * 成交库页面
 */

public class FindTransactionFragment extends BaseFragment implements OnRefreshLoadMoreListener {

    private SmartRefreshLayout mSmartRefreshLayout;
    private RecyclerView mRecyclerView;
    private FindTransactionAdatper mAdapter;
    private List<FindTanListBean.DataBean> data;
    private UserInfoBean userInfoBean;

    public static FindTransactionFragment getInstance(String type) {
        FindTransactionFragment findTabListFragment = new FindTransactionFragment();
        Bundle args = new Bundle();
        args.putString("type", type);
        findTabListFragment.setArguments(args);
        return findTabListFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_find_transaction;
    }


    @Override
    public void initView(View view) {
        mSmartRefreshLayout = (SmartRefreshLayout) view.findViewById(R.id.mSmartRefreshLayout_tr);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.fragment_find_transaction);

        mSmartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        mSmartRefreshLayout.setOnRefreshListener(refreshlayout -> {
            boolean mobileConnected = NetUtil.isNetworkAvailable(SkinApplication.sContext);
            if (mobileConnected) {
                initData(userInfoBean.getUser().getCrm_user_id(),"");
            } else {
                ToastHelper.showToast("网络未连接，请检查网络状态");
                if (mSmartRefreshLayout!= null){
                    mSmartRefreshLayout.finishRefresh();
                }
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //为RecyclerView设置分割线(这个可以对DividerItemDecoration进行修改，自定义)
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        //动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mAdapter = new FindTransactionAdatper(R.layout.fragment_find_transaction_item);
        mAdapter.bindToRecyclerView(mRecyclerView);

        userInfoBean = CacheUserInfo.getInstance().getsUserInfo();
        initData(userInfoBean.getUser().getCrm_user_id(),"");

    }
    public void initData(int crmuserid,String name){
        showLoadingDialog();
        RetrofitManger.getInstance().createService(UserApi.class)
                .gettranlist(crmuserid,name)
                .compose(RxSchedulers.<ResponseEntity<FindTanListBean>>applySchedulers())
                .subscribe(new ResponseObserve<FindTanListBean>() {

                    @Override
                    public void onSuccess(ResponseEntity<FindTanListBean> responseEntity) {
                        data = responseEntity.getData().getData();
                        mAdapter.setNewData(data);

                    }

                    @Override
                    public void onFail(String errorMsg) {
                        if (errorMsg != null&&!errorMsg.equals("")){
                            ToastHelper.show(errorMsg);
                        }else {
                            ToastHelper.show("服务器繁忙，请稍后再试");
                        }
                    }

                    @Override
                    public void onSubscribeStart(@io.reactivex.annotations.NonNull Disposable disposable) {
                        addDisposable(disposable);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        closeLoadingDialog();
                        if (mSmartRefreshLayout!= null){
                            mSmartRefreshLayout.finishRefresh();
                        }
                    }
                });

    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) { }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) { }
}
